/**
 * @Title: warning_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @date 2014年10月27日 下午4:43:38
 * @version V1.0
 */
;
(function(){
    'use strict';

    var app = angular.module('fwa.mainApp');
    app.controller('UpdateDrugStoreCtrl', ['$scope','CommunityService','UpdateDrugStoreService','$state','BaseURL','Modal','FwaTabsetService','$stateParams',
        function($scope,CommunityService,UpdateDrugStoreService,$state,baseUrl,Modal,fwaTabsetService,$stateParams){

    		//页面
    		$scope.includeUrl = 'views/hcpms/evaluation/evaluationDrugStore/drugStoreIntgrity/checkTableDrugStore.html';

            $scope.intgrityEvalTypeInfo={};
            //tab切换事件
            $scope.tabClick = function(data) {
                $scope.intgrityEvalTypeInfo = data;
                $scope.selectedTab = data;
                loadIntgrityEvalDetailInfo(data.evaluRuleId,data.intgrityEvalChildId);
             }
            //初始化请求参数
            var evaluationCode=null;
            if($stateParams.evaluationCode){
                evaluationCode = $stateParams.evaluationCode;
            }
            /**
             * 初始化诚信单位考评详情
             */
            $scope.intgrityEvalData = {};
            function loadIntgrityEvalData() {
                var parameter={evaluationCode:evaluationCode};
                UpdateDrugStoreService.loadIntgrityEvalInfo(parameter).success(function(response){
                   if(response.state){
                       $scope.intgrityEvalData = response.result.intgrityEvalInfo;
                  }
               });
            }
            /**
             * 初始化诚信考评类别详情
             */
            $scope.tabs = [];
            function loadIntgrityEvalTypeInfo() {
                var parameter = {evaluationCode:evaluationCode};
                UpdateDrugStoreService.loadIntgrityEvalTypeInfo(parameter).success(function(response){
                    if(response.state){
                    	$scope.tabs = response.result.intgrityEvalTypeInfo;
                    	console.log($scope.tabs);
                    	 //第一次默认选中第一个tab页
                        $scope.tabClick($scope.tabs[0]);
                    }
                });
            }
            
            var intgrityEvalMap = {};
            /**
             * 初始化诚信考评详情
             */
            $scope.intgrityEvalInfo = {};
            function loadIntgrityEvalDetailInfo(evaluRuleId,intgrityEvalChildId) {
                var parameter= {evaluRuleId:evaluRuleId,intgrityEvalChildId:intgrityEvalChildId};
                if (!intgrityEvalMap[evaluRuleId] || intgrityEvalMap[evaluRuleId].isFlag) {
                	$scope.loading=true;
                	UpdateDrugStoreService.loadIntgrityEvalDetailInfo(parameter).success(function(response){
                		$scope.loading=false;
                        if (response.state) {
                            $scope.intgrityEvalInfo = response.result.data;
                            intgrityEvalMap[evaluRuleId] = response.result.data;
                            intgrityEvalMap[evaluRuleId].isFlag = false;
                            getTableDatas(intgrityEvalMap[evaluRuleId]);
                        }
                    });
                }
                else {
                	$scope.intgrityEvalInfo = intgrityEvalMap[evaluRuleId];
                	getTableDatas(intgrityEvalMap[evaluRuleId]);
                }
                
            }

            //json 数据重新组合
            function getTableDatas(datas) {
                if (datas) {
                    var tableDatas = [];
                    angular.forEach(datas,function(oneData,i) {
                        var oneLength = 0;
                        oneData.no = i + 1;
                        tableDatas.push(oneData);
                        angular.forEach(oneData.list,function(twoData,j) {
                            twoData.rowSpan = twoData.list.length;
                            if (j == 0) {
                                oneData.data = twoData;
                            }
                            else {
                                twoData.oneHide = true;
                                tableDatas.push(twoData);
                            }
                            if (twoData.list && twoData.list.length > 0 ) {
                                angular.forEach(twoData.list,function(data,k) {
                                    oneLength++;
                                    if (data.isConfirm == '0') {
                                        data.checked = true;
                                    }
                                    else {
                                        data.checked = true;
                                    }
                                    if (k == 0) {
                                        twoData.data = data;
                                    }
                                    else {
                                        data.oneHide = true;
                                        data.twoHide = true;
                                        tableDatas.push(data);
                                    }
                                });
                            }
                            else {
                                oneLength++;
                            }
                        });
                        oneData.rowSpan = oneLength;
                    });
                    $scope.tableData = tableDatas;
                }
            }


            //获取所有的扣分细则以及扣分值 进行保存操作
            function gainSelectValue(datas) {
                if (datas) {
                    var selectMark = [];
                    var isFalg = false;
                    var isSelectNum = 0;
                    for (var i = 0;i < datas.length; i++) {
                    	var oneData = datas[i];
                    	 for (var j = 0;j < oneData.list.length; j++) {
                    		 var twoData = oneData.list[j];
                    		 var isSelect = false;
                     		 var markInfoList = [];
                    		 for (var k = 0;k < twoData.list.length; k++) {
                    			 var data = twoData.list[k];
                    			 var markInfoData = {
                         				markValueId:data.id
                         		};
                             	if (data.checked) {
                             		isSelect = true;
                             		markInfoData.isConfirm = '0';
                             	}
                             	else {
                             		markInfoData.isConfirm = '1';
                             	}
                             	markInfoList.push(markInfoData);
                        	 }
                    		 if (isSelect) {
                     			if (!twoData.evaluationUser) {
                     				Modal.alert("请输入扣分细则“"+twoData.markContent+"”所对应的考核人!");
                     				isFalg = true;
                     				break;
                     			}
                     			isSelectNum++;
                     		}
                     		else {
                     			if (twoData.evaluationUser) {
                     				Modal.alert("请选中扣分细则“"+twoData.markContent+"”所对应需要扣分值的选项!");
                     				isFalg = true;
                     				break;
                     			}
                     		}
                     		var markDetailValue = {};
             				markDetailValue.markDetailValueId = twoData.id;
                 			markDetailValue.evaluationUser = twoData.evaluationUser;
                 			markDetailValue.list = markInfoList;
                 			selectMark.push(markDetailValue);
                    	 }
                    }
                    if (isFalg) {
                    	return isFalg;
                    }
                    else {
                    	if (isSelectNum == 0) {
                    		Modal.alert("请选中一项再进行操作!");
                    		return true;
                    	}
                    	return selectMark;
                    }
                    
                }
            }
            /**
             * 返回
             */
            $scope.goBack = function() {
                $state.go("drugStoreIntgrityManager.drugStoreIntgrityList");
            };
            /**
             * 暂存或保存
             */
            $scope.comfirmInfo = {};
            $scope.submitForEvaluation = function(flag) {
                if(!$scope.intgrityEvalTypeInfo.evalDate){
                    Modal.alert("考评时间为必填项!");
                    return false;
                }
                if(!$scope.intgrityEvalTypeInfo.evaluationTeamLeader){
                    Modal.alert("考核小组负责人为必填项!");
                    return false;
                }
                var selectMark = gainSelectValue(intgrityEvalMap[$scope.selectedTab.evaluRuleId]);
                if (selectMark == true) {
                    return false;
                }
            	$scope.intgrityEvalTypeInfo.evalMarkDeatilInfoList = selectMark;
            	$scope.intgrityEvalTypeInfo.onlySave = flag;
                $scope.intgrityEvalTypeInfo.evaluationCode=evaluationCode;
            	intgrityEvalMap[$scope.selectedTab.evaluRuleId].isFlag = true;
            	$scope.loading = true;
            	UpdateDrugStoreService.saveIntgrityEvalInfo($scope.intgrityEvalTypeInfo).success(function(response){
                	 $scope.loading = false;
                    if(response) {
                      Modal.alert(response.msg);
                        if (response.flag) {//保存，跳转页面
                            $scope.goBack();
                        }
                    }
                   
                });
            }
            
            loadIntgrityEvalData();
            loadIntgrityEvalTypeInfo();
            
        }]);
})();
